package com.sinaapp.mspring.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.sinaapp.mspring.annotation.SystemServiceLog;
import com.sinaapp.mspring.mapper.PopulationMapper;
import com.sinaapp.mspring.model.pojo.Population;
import com.sinaapp.mspring.service.PopulationServiceI;


/**
 * 中国人口统计业务实现
 * @author binchao
 * 2016年3月13日
 */
@Service("populationService")
public class PopulationServiceImpl extends BaseServiceImpl<Population, Integer> implements PopulationServiceI {

	@Autowired
	private PopulationMapper popMapper;
	
	@Autowired
	public void setBaseMapper() {
		super.setBaseMapper(popMapper);
	}

	@Override
	@SystemServiceLog(description = "查询所有人口统计信息")
	public List<Population> selectAll() {
		return popMapper.selectAll();
	}

	@Override
	@SystemServiceLog(description = "生成人口统计图表option")
	public String getPopulationForEchartsOption() {
		//获取人口统计数据
				List<Population> populations = selectAll();
				//设置echarts option
				GsonOption option = new GsonOption();
				
				option.title("全国人口统计数据", "来自国家数据");
		        option.tooltip().trigger(Trigger.axis);
		        option.legend("总人口", "男性人口", "女性人口", "城镇人口", "乡村人口");
		        option.toolbox().show(true).feature(Tool.mark,
		                Tool.dataView,
		                new MagicType(Magic.line, Magic.bar, Magic.stack, Magic.tiled),
		                Tool.restore,
		                Tool.saveAsImage).padding(20);
		        //年份作为横轴
				List<String> xAxis = new ArrayList<>();
				//总人口
				List<Integer> totalPop = new ArrayList<>();
				//男性人口
				List<Integer> manPop = new ArrayList<>();
				//女性人口
				List<Integer> womenPop = new ArrayList<>();
				//城镇人口
				List<Integer> townPop = new ArrayList<>();
				//乡村人口
				List<Integer> ruralPop = new ArrayList<>();
				for(Population p : populations) {
					xAxis.add(p.getYear());
					totalPop.add(p.getTotal());
					manPop.add(p.getMan());
					womenPop.add(p.getWomen());
					townPop.add(p.getTownpop());
					ruralPop.add(p.getRuralpop());
					
				}
		        option.calculable(true);
		        option.xAxis(new CategoryAxis().boundaryGap(false).data(xAxis.toArray()));
		        option.yAxis(new ValueAxis());
		        
				Line total = new Line("总人口");
				//total.smooth(true).itemStyle().normal().areaStyle().typeDefault();
				total.data(totalPop.toArray());
				
				Line man = new Line("男性人口");
				//man.smooth(true).itemStyle().normal().areaStyle().typeDefault();
				man.data(manPop.toArray());
				
				Line women = new Line("女性人口");
				//women.smooth(true).itemStyle().normal().areaStyle().typeDefault();
				women.data(womenPop.toArray());
				
				Line town = new Line("城镇人口");
				//town.smooth(true).itemStyle().normal().areaStyle().typeDefault();
				town.data(townPop.toArray());
				
				Line rural = new Line("乡村人口");
				//rural.smooth(true).itemStyle().normal().areaStyle().typeDefault();
				rural.data(ruralPop.toArray());
				
				option.series(total, man, women, town, rural);
		        
				return option.toString();
	}

}
